/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_te_header_unpack.c                             */
/*  NOTE      =                                                    */
/*  DATE      = 2017/02/24 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>

#include "frcomm.h"

#include <tongeasy/config.h>
#include <tongeasy/te_core.h>
#include <tongeasy/te_header.h>
#include <tongeasy/te_comm.h>
#include <apmtest_comm.h>

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    char *p_bin = (char *) "cf0700000a6fce009325000064656d6f73767200000000000064656d6f636c690000000000000000b2080000355a303230307c30303037303030307c30303434303030307c64656d6f636c69202020202064656d6f636c69202020202064656d6f73767220202020203733202020203020202064656d6f636c6920202020203020202000000000000000000000000063616c6c5f7265715f66756e3120202020202020303032312020302020203020202031302020333020207468697320636c69656e742073796e632064617461";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    TE_header st_te_header;
    memset(&st_te_header, 0x00, sizeof(st_te_header));

    CU_ASSERT_EQUAL(te_header_unpack(arr_payload, strlen(p_bin) / 2, &st_te_header), FR_SUCC);
    CU_ASSERT_EQUAL(st_te_header.ui_header_len, 185);
    CU_ASSERT_EQUAL(st_te_header.ui_pkt_len, 206);
    CU_ASSERT_EQUAL(st_te_header.pkt_type, TE_PKT_TYPE_SYNC_ASYNC_REQ);
    CU_ASSERT_EQUAL(st_te_header.i_ptxflag, 0x00070000);
    CU_ASSERT_EQUAL(st_te_header.i_branchflag, 0x00440000);
    CU_ASSERT_EQUAL(st_te_header.i_tid, 73);
    CU_ASSERT_EQUAL(st_te_header.i_subtid, 0);
    CU_ASSERT_STRING_EQUAL(st_te_header.arr_original_node, "democli");
    CU_ASSERT_STRING_EQUAL(st_te_header.arr_send_node, "democli");
    CU_ASSERT_STRING_EQUAL(st_te_header.arr_recv_node, "demosvr");
    CU_ASSERT_STRING_EQUAL(st_te_header.arr_svcname, "call_req_fun1");
    CU_ASSERT_EQUAL(st_te_header.s_ret_code, 0);
    CU_ASSERT_EQUAL(st_te_header.i_user_data_len, 21);
    CU_ASSERT_EQUAL(st_te_header.i_filename_len, 0);
    CU_ASSERT_EQUAL(st_te_header.i_key_data_len, 0);
}

int add_suite_te_header_unpack(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" te_header_unpack()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
